#include "smarthome.h"
#include "ui_smarthome.h"

smarthome::smarthome(QString pid,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::smarthome)
{   this->setWindowTitle("控制台");

    ui->setupUi(this);
    id=pid;
    mqtt_init();
    light.bedroom=OFF;
    light.toletroom=OFF;
    light.livingroom=OFF;
    light.kitchenroom=OFF;
    uiinit();
}

smarthome::~smarthome()
{
    delete ui;
}
void  smarthome::connect_server_success()
{

    qDebug()<<"connect mqtt server success";

    connect(client,&QMqttClient::disconnected,[this]
    {

         QMessageBox::warning(this,"连接提示","mqtt断开连接");


    });

    connect(client,&QMqttClient::messageReceived,this,&smarthome::message_receive);

    QString topic;
    qDebug()<<id;
    topic+="upload";
    topic+=id;

    client->subscribe(topic);

}
void  smarthome::uiinit()
{
      ui->year->setFont(QFont("微软雅黑",16));
      ui->week->setFont(QFont("微软雅黑",16));
      ui->time->setFont(QFont("微软雅黑",16));

       time=new QTimer;
       time->start(1000);

       connect(time,&QTimer::timeout,this,&smarthome::timedeal);





}


 void   smarthome::message_receive(const QByteArray &msg,const QMqttTopicName &topic)
 {

     qDebug()<<topic<<msg;

      QJsonObject obj=QJsonDocument::fromJson(msg).object();
       qDebug()<<obj<<" ";
      int light=obj.value("light").toInt();
      int tmp=obj.value("tmp").toInt();
      int humi=obj.value("hum").toInt();
      qDebug()<<light<<" "<<tmp<<" "<<humi;
      QString tmpstr=QString::number(tmp);
      tmpstr+="℃";
      QString humistr=QString::number(humi);
      humistr+="%";

      ui->light->setText(QString::number(light));
      ui->tmp->setText(tmpstr);
      ui->hum->setText(humistr);

 }
void   smarthome::mqtt_init()
  {
    client =new QMqttClient;
    client->setHostname("47.121.24.16");
    client->setPort(1884);

    connect(client,&QMqttClient::connected,this,&smarthome::connect_server_success);
    client->connectToHost();






  }


void smarthome::on_livingroom_clicked()
{
      QJsonObject obj;

    if(light.livingroom==OFF)
    {
          obj.insert("cmd","light");
          obj.insert("which",LIVINGROOM);
          obj.insert("stat",ON);
         client->publish(id,QJsonDocument(obj).toJson());
         light.livingroom=ON;

    }

     else
     {

        obj.insert("cmd","light");
        obj.insert("which",LIVINGROOM);
        obj.insert("stat",OFF);


        client->publish(id,QJsonDocument(obj).toJson());

        light.livingroom=OFF;

    }
}

void smarthome::on_toletroom_clicked()
{
    QJsonObject obj;
   //qDebug()<<"到这了";
  if(light.toletroom==OFF)
  {
        obj.insert("cmd","light");
        obj.insert("which",TOLETROOM);
        obj.insert("stat",ON);

       // qDebug()<<"已打印";
        client->publish(id,QJsonDocument(obj).toJson());

        light.toletroom=ON;

  }

   else
   {

      obj.insert("cmd","light");
      obj.insert("which",TOLETROOM);
      obj.insert("stat",OFF);


      client->publish(id,QJsonDocument(obj).toJson());

      light.toletroom=OFF;

  }
}

void smarthome::on_bedroom_clicked()
{
    QJsonObject obj;
   //qDebug()<<"到这了";
  if(light.bedroom==OFF)
  {
        obj.insert("cmd","light");
        obj.insert("which",BEDROOM);
        obj.insert("stat",ON);

       // qDebug()<<"已打印";
        client->publish(id,QJsonDocument(obj).toJson());

        light.bedroom=ON;

  }

   else
   {

      obj.insert("cmd","light");
      obj.insert("which",BEDROOM);
      obj.insert("stat",OFF);


      client->publish(id,QJsonDocument(obj).toJson());

      light.bedroom=OFF;

  }
}

void smarthome::on_kichenroom_clicked()
{
    QJsonObject obj;
   //qDebug()<<"到这了";
  if(light.kitchenroom==OFF)
  {
        obj.insert("cmd","light");
        obj.insert("which",KITCHENROOM);
        obj.insert("stat",ON);

       // qDebug()<<"已打印";
        client->publish(id,QJsonDocument(obj).toJson());

        light.kitchenroom=ON;

  }

   else
   {

      obj.insert("cmd","light");
      obj.insert("which",KITCHENROOM);
      obj.insert("stat",OFF);


      client->publish(id,QJsonDocument(obj).toJson());

      light.kitchenroom=OFF;

  }
}

void smarthome::on_wuroom_clicked()
{
    QJsonObject obj;

  if(light.buzzer==OFF)
  {
        obj.insert("cmd","buzzer");

        obj.insert("stat",ON);


        client->publish(id,QJsonDocument(obj).toJson());

       light.buzzer=ON;

  }

  else
   {
      obj.insert("cmd","buzzer");

      obj.insert("stat",OFF);


      client->publish(id,QJsonDocument(obj).toJson());

      light.buzzer=OFF;




  }
}

void smarthome::on_tabWidget_tabBarClicked(int index)
{    QJsonObject obj;
    if(index==0)
    {

        obj.insert("cmd","menu");

        obj.insert("tab","1");


        client->publish(id,QJsonDocument(obj).toJson());




    }
    else if(index==1)
    {
        obj.insert("cmd","menu");

        obj.insert("tab","2");


        client->publish(id,QJsonDocument(obj).toJson());





    }
    else if(index==2)
    {
        obj.insert("cmd","menu");

        obj.insert("tab","3");


        client->publish(id,QJsonDocument(obj).toJson());





    }



}
void  smarthome::timedeal()
{
   QDateTime curtime=QDateTime::currentDateTime();


   int year=curtime.date().year();
   int month=curtime.date().month();
   int day=curtime.date().day();
   int week=curtime.date().dayOfWeek();

   int hour=curtime.time().hour();
   int min=curtime.time().minute();
   int sec=curtime.time().second();

   QString yearstr=QString::number(year);
     yearstr+='-';
     if(month<10)
     {
          yearstr+='0';
          yearstr+=QString::number(month);



     }
     else
     {
           yearstr+=QString::number(month);

     }
      yearstr+='-';
      if(day<10)
      {

          yearstr+='0';
          yearstr+=QString::number(day);

      }
      else
      {
           yearstr+=QString::number(day);


      }


    ui->year->setText(yearstr);

    QString weekstr="星期";
    switch(week)
    {
      case 1:
           weekstr+="一";
          break;
      case 2:
            weekstr+="二";
        break;
      case 3:
            weekstr+="三";
        break;
      case 4:
            weekstr+="四";
        break;

     case 5:
            weekstr+="五";
        break;

    case 6:
            weekstr+="六";
        break;

    case 7:
            weekstr+="天";
        break;


    }
     ui->week->setText(weekstr);

     QString timestr;

      if(hour<10)
      {timestr+='0';
       timestr+=QString::number(hour);}
      else
      {
          timestr+=QString::number(hour);

      }

      timestr+=':';

      if(min<10)
      {timestr+='0';
       timestr+=QString::number(min);}
      else
      {
          timestr+=QString::number(min);

      }

        timestr+=':';
        if(sec<10)
        {timestr+='0';
         timestr+=QString::number(sec);}
        else
        {
            timestr+=QString::number(sec);

        }
            ui->time->setText(timestr);
}

void smarthome::on_timeEdit_editingFinished()
{
      QJsonObject obj;
     int hour=ui->timeEdit->time().hour();
     int min=ui->timeEdit->time().minute();

     obj.insert("cmd","alarm");

     obj.insert("hour",hour);
     obj.insert("min",min);


     client->publish(id,QJsonDocument(obj).toJson());


}
